package cn.btks.resultintegration.catalog.service.impl;

import cn.btks.resultintegration.catalog.dto.CatalogWithFileDto;
import cn.btks.resultintegration.catalog.dto.CatalogWithFileTreeDto;
import cn.btks.resultintegration.catalog.dto.FileCatalogDto;
import cn.btks.resultintegration.catalog.pojo.FileCatalog;
import cn.btks.resultintegration.catalog.pojo.PolicyFile;
import cn.btks.resultintegration.catalog.repository.FileCatalogRepository;
import cn.btks.resultintegration.catalog.repository.PolicyFileRepository;
import cn.btks.resultintegration.catalog.service.FileCatalogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2020/12/14 17:17
 */
@Service
@Transactional
public class FileCatalogServiceImpl implements FileCatalogService {

    @Autowired
    FileCatalogRepository catalogRepository;

    @Autowired
    PolicyFileRepository fileRepository;

    @Override
    public List<FileCatalogDto> findAll() {
        List<FileCatalog> catalogList = catalogRepository.findAll();
        List<FileCatalog> rootFileCatalog = new ArrayList<>();
        if (catalogList != null && catalogList.size() > 0) {
            for (FileCatalog item : catalogList) {
                if (item.getLevel() == 1) {
                    rootFileCatalog.add(item);
                }
            }
        }
        List<FileCatalogDto> catalogDtoList = new ArrayList<>();
        if (rootFileCatalog.size() > 0) {
            for (FileCatalog item : rootFileCatalog) {
                FileCatalogDto catalogDtos = recursion(item);
                catalogDtoList.add(catalogDtos);
            }
        }
        return catalogDtoList;
    }

    @Override
    public List<CatalogWithFileTreeDto> findCatalogWithFileTree() {
        List<FileCatalog> catalogList = catalogRepository.findAll();
        List<FileCatalog> rootFileCatalog = new ArrayList<>();
        if (catalogList != null && catalogList.size() > 0) {
            for (FileCatalog item : catalogList) {
                if (item.getLevel() == 1) {
                    rootFileCatalog.add(item);
                }
            }
        }
        List<CatalogWithFileTreeDto> result = new ArrayList<>();
        if (rootFileCatalog.size() > 0) {
            CatalogWithFileDto dto = new CatalogWithFileDto();
            for (FileCatalog item : rootFileCatalog) {
                CatalogWithFileTreeDto catalogDtos = recursion1(item);
                result.add(catalogDtos);
            }
        }
        return result;
    }

    @Override
    public List<CatalogWithFileTreeDto> findCatalogWithFileTreeByCatalogId(Long id) {
        List<CatalogWithFileTreeDto> result = new ArrayList<>();
        Optional<FileCatalog> optional = catalogRepository.findById(id);
        if(optional.isPresent()){
            FileCatalog fileCatalog = optional.get();
            CatalogWithFileTreeDto catalogDtos = recursion1(fileCatalog);
            result.add(catalogDtos);
        }
        return result;
    }


    /**
     * 递归查询目录
     *
     * @param catalog
     * @return
     */
    private FileCatalogDto recursion(FileCatalog catalog) {
        FileCatalogDto catalogDto = new FileCatalogDto();
        catalogDto.setParent(catalog);
        List<FileCatalogDto> children = new ArrayList<>();
        catalogDto.setChildren(children);
        Long id = catalog.getId();
        List<FileCatalog> catalogList = catalogRepository.findAllByUpLevelId(id);
        if (catalogList != null && catalogList.size() > 0) {
            for (FileCatalog item : catalogList) {
                FileCatalogDto childrenItem = recursion(item);
                children.add(childrenItem);
            }
        }
        return catalogDto;
    }

    /**
     * 递归查询目录
     *
     * @param catalog
     * @return
     */
    private CatalogWithFileTreeDto recursion1(FileCatalog catalog) {
        CatalogWithFileTreeDto catalogDto = new CatalogWithFileTreeDto();
        CatalogWithFileDto catalogWithFileDto = new CatalogWithFileDto();
        catalogWithFileDto.setFileCatalog(catalog);
        List<PolicyFile> policyFiles = fileRepository.findAllByCatalogId(catalog.getId());
        if(CollectionUtils.isNotEmpty(policyFiles)){
            catalogWithFileDto.setFilesList(policyFiles);
        }
        catalogDto.setParent(catalogWithFileDto);
        List<CatalogWithFileTreeDto> children = new ArrayList<>();
        catalogDto.setChildren(children);
        Long id = catalog.getId();
        List<FileCatalog> catalogList = catalogRepository.findAllByUpLevelId(id);
        if (catalogList != null && catalogList.size() > 0) {
            for (FileCatalog item : catalogList) {
                CatalogWithFileTreeDto childrenItem = recursion1(item);
                children.add(childrenItem);
            }
        }
        return catalogDto;
    }
}
